﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace tp4_montecarlo
{
    class Demanda
    {

        private List<Item_Demanda> lstDemanda;

        public Demanda()
        {
            lstDemanda = new List<Item_Demanda>();
            //inicializo la tabla de demanda
            Item_Demanda i5 = new Item_Demanda(450, (double)10 / 100);
            Item_Demanda i1 = new Item_Demanda(250, (double)30/100);
            Item_Demanda i2 = new Item_Demanda(300, (double)25/100);
            Item_Demanda i3 = new Item_Demanda(350, (double)20/100);
            Item_Demanda i4 = new Item_Demanda(400, (double)15/100);
           
            lstDemanda.Add(i1);
            lstDemanda.Add(i2);
            lstDemanda.Add(i3);
            lstDemanda.Add(i4);
            lstDemanda.Add(i5);
            //Ordeno la lista
            lstDemanda.Sort(delegate(Item_Demanda item1, Item_Demanda item2)
                            {
                                return item1.CompareTo(item2);
                            });

            calcularIntervalos();

        }

        private void calcularIntervalos()
        {
            double minimo = 0;
            foreach (Item_Demanda item in lstDemanda)
            {
                item.Min_interval = minimo;
                item.Max_interval = minimo + item.Probabilidad * 100-1;
                minimo = item.Max_interval+1;                
            } 
        }

        //Este método debe devolver la demanda de acuerdo al random que ingresa por parámetro
        public int getDemanda(double random_number)
        {
            //int random = (int)(random_number * 100);
            int random = (int)random_number;

            foreach (Item_Demanda item in lstDemanda)
            {
                if (random >= item.Min_interval && random <= item.Max_interval)
                {
                    return item.Unidades;
                }                
            }
            return -1; 
        }
    }
}
